#!/bin/bash 

source head.sh

# 接收学生ID，查询用户名
user_id=$(echo $QUERY_STRING | awk -F "=" '{print $2}')
user_name=$($mysqllogin "select user_name from lab_user where user_id=$user_id" | grep -v user_name)

# 获取学生所在可用区的域名和vpn端口
regionhost=$($mysqllogin "select concat(lab_region.domain, ' ', lab_region.vpnport) from lab_user join lab_region on lab_user.region=lab_region.id where lab_user.user_id=$user_id;" | grep -v vpnport)

echo "HTTP/1.1 200 OK"
echo "Content-Type: application/octet-stream"
echo "Content-Disposition: attachment;filename=$user_name.ovpn"
echo "Connection: keep-alive"
echo ""

echo "
client
nobind
dev tun
remote-cert-tls server
auth-user-pass
remote $regionhost udp
float
route 10.16.0.0 255.255.0.0 vpn_gateway
route 10.10.10.0 255.255.255.0 vpn_gateway

<key>
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC0MioxrxeMi/JH
2lrO/9SCQAuxWZvyjsXhrVueUwUicuclYBPmPCjF2NRfZztlr1ZuVmPcWPtwhvhU
gpxbsu66SCk9Ba4YRonL7b4PL6kwbINv5z3JYWXO5x8ymBUXla5cQ4rKji1vs4WD
AgaGLgyKe1+s1vPooBmdtnRdb1OOKLzh7/oUG1BKs37TgqAw9/BCqKKJ3wc/VVur
u6PKT47LZdPqiHXWc2YCK/b0RnHC/sBbaVBwmhK12uXfZCFbH7wRCcQaSvKi1VFk
8nDBBeL0IJxW3r3g2iOmlj/EbxLdV28LqG4lHz7dRbpoUR62DfCsQ5XiGdGPJUSD
fIA8pdyZAgMBAAECggEAWKXJLjIWIrWCVqjsPaOL9xvs5K3R0207mj9/sA7lmsHu
RuvSPlTgNhzqtZiClw7ANX3YmMFJyV0M8/5hT5dibFAtwXB26khhZ+qosq3ezQzF
SJr5QYXh4PzA01IQJk8jvQ4ZTHbPkYE+sGmhrgcfck0pj00cJxgEIhtteb3X3DoI
jfshM/oVBOAzw0u+w3oOzvAt7FtOixwINyw6Hflk0k3gMX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Bge7vBRHYqZgdBQO6DA2U44+8gWN9zvpXrsC3tidVUfvT
xMxwmbJ4BgS2zBqkLE6TnoZg74pAJZA05xOy533fOgOYKVQhSov2PWLLaTY6jqWR
9cDECzG7E55W2sBA78dSTHldNlhxvJl5PsLCzWQPE7KZKdKdzrXCZj66svxPN1aL
w6syJeTAyorgSA==
-----END CERTIFICATE-----
</ca>
key-direction 1
<tls-auth>
#
# 2048 bit OpenVPN static key
#
-----BEGIN OpenVPN Static key V1-----
05c957fd11458008ab9b1cfa761d3673
26833852e62a1b0fc60acd797d4dbccf
26d57d8893282a8d2130f5bfb3fcc8af
1e7b3d6423d45af4209d0d823340b6d0
46b8a2f94c1ffb7334dad37c97745d6b
ad7d3d3fdca03067aaf6f5ec12526ae3
c02b9c6ee41140b02665ce9bd40b6c10
be7bcafed4c1776c43ac21c627dbf598
ae6840918e8a2407d482fb2274255c56
683c15499aa0452025b9c62f0fcf03aa
4f6dc7727703cb78f6c46faa484ed377
39f0c538a6bb6d642923f7c84cfcb6da
76a68a5444d583fc702081d15904cdc1
9494f716248e1b27393435e1eca7e55a
2d0446c3d6a57689b32ca7e4a3bfc0cb
8881f7793d8b6af5f40e89c4323350bc
-----END OpenVPN Static key V1-----
</tls-auth>

# redirect-gateway def1"

